// author by welong
// zuowl@qq.com
// 2014-09-04  10:22
// PAT basic 1003

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void func(char *str)
{
	int i;
	int a, b, c;	//store the count of a(A before P), b(between P & T), c(after T)
	int count_p = 0, count_t = 0;

	for(i = 0; str[i] != 0; i++)
	{
		if(str[i] == 'A')
			continue;
		else if(str[i] == 'P')
		{
			a = i;
			count_p++;
		}
		else if(str[i] == 'T')
		{
			b = i;
			count_t++;
		}
		else
		{
			printf("NO\n");
			return;
		}
	}

	c = i - (b+1);
	b = b - (a+1);	//b can value 1-n, if only (b==1 || b==2) cause test site 1,4 error

	if(count_p == 1 && count_t == 1 && (c == b*a && b != 0))
		printf("YES\n");
	else
		printf("NO\n");
}
int main(int argc, char **argv)
{
	int  i, count;
	char str[101];

	scanf("%d", &count);
	for(i = 0; i < count; i++)
	{
		scanf("%s", str);
		func(str);
	}
	return 0;
}